package com.epoint.mobileoa.ui.update;

import java.io.File;
import java.util.HashMap;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.View;
import android.widget.Button;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.FrameMethod;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.bizlogic.mobileoa.UpdateClientSoftwareTask;
import com.epoint.mobileoa.http.HttpHelp;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.AlertHelp;
import com.epoint.mobileoa.tools.GetRemoteInfo;
import com.epoint.mobileoa.tools.StringHelp;
import com.epoint.mobileoa.ui.init.InitView;

/**
 * @author liyc
 * @time 2012-2-4 下午9:07:46
 * @annotation 软件更新
 */
public class UpdateView extends SuperView {

	public static final int UpdateClientSoftware = 1;

	private Button bt;

	@Override
	public void initControls() {
		bt = (Button) findViewById(R.id.uv_bt);
		bt.setOnClickListener(this);

		int enterfalg = getIntent().getIntExtra("enterfalg", 0);
		if(enterfalg==1){
			String url = getIntent().getStringExtra("url");
			String fullfilename = getIntent().getStringExtra("fullfilename");
			attachdownload(fullfilename,url);
		}
	}

	@Override
	public void onClick(View v) {
		if(v==bt){
			top_pb.setVisibility(View.VISIBLE);
			checkUpdate(this);
		}
	}

	public static void checkUpdate(SuperView con){
		Task task = new UpdateClientSoftwareTask(con,new HashMap<String,Object>());

		MainService.addTask(task);
	}

	@Override
	public void refresh(Object... param) {
		if(param[0].toString().equals(MainService.NORMAL)){
			String bs = param[1].toString();
			if(bs.contains(HttpHelp.CONNECTION_TIME_OUT)){
				AlertHelp.ShowAlert(this, "当前没有可用更新！");
			}
			else{
				String versionStr = StringHelp.getXMLAtt(bs, "version");
				String name = StringHelp.getXMLAtt(bs, "name");
				int remoteversion = FrameMethod.versionToInteger(versionStr);
				int localversion = FrameMethod.versionToInteger(getString(R.string.version));
				final String fullfilename = name+"_V"+versionStr+".apk";
				final String clientname = StringHelp.getXMLAtt(bs, "clientname");
				if(remoteversion>localversion){
					AlertHelp.ShowAlert(this, "检测到可用的升级软件"+fullfilename+"，是否立即更新？", new OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							String url = FrameMethod.getUpdateAPKUrl(fp, UpdateView.this, clientname);
							attachdownload(fullfilename,url);
						}
					});
				}
				else{
					AlertHelp.ShowAlert(this, "版本号:"+getString(R.string.version)+"\n当前是最新版本！");
				}
			}
		}
		top_pb.setVisibility(View.INVISIBLE);
	}

	private void attachdownload(String filename,String fileurl){
		ProgressDialog mProgressDialog = null;
		String attachfilepathpix = InitView.MobileOaMainStoreFolder+"/"+getString(R.string.sdforder)+"/update/";
		File folder = new File(attachfilepathpix);
		if(!folder.exists()){
			folder.mkdirs();
		}
		String attachfilepath = attachfilepathpix+"/"
				+ filename;
		new GetRemoteInfo(UpdateView.this,
				attachfilepath, mProgressDialog)
		.executeBefore(fileurl);
	}

	@Override
	public int getLayoutInt() {
		return R.layout.updateview;
	}

	@Override
	public String setTitle() {
		return getString(R.string.rjgx);
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param) {
	}

}
